﻿using _14Net8AspNetCore_Authorization.Context;
using _14Net8AspNetCore_Authorization.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace _14Net8AspNetCore_Authorization.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly AuthorizationDbContext _DbContext;

        public ProductController(AuthorizationDbContext dbContext)
        {
            _DbContext = dbContext;
        }


        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(ClaimTypes.Name)]
        public async Task<IActionResult> AddProduct(Product product)
        {
            _DbContext.products.Add(product);
            _DbContext.SaveChanges();
            return Ok("添加成功");
        }

        /// <summary>
        /// 查询商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize("permisson")]
        public async Task<IActionResult> GetProduct()
        {
            List<Product> products = _DbContext.products.Select(T => T).ToList();
            return Ok(products);
        }

        /// <summary>
        /// 查询商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize("Delete")]
        public async Task<IActionResult> DeleteProduct()
        {
            
            return Ok("删除成功");
        }
    }
}
